package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaNoticeAddDTO;
import com.cheer.dto.OaNoticeUpdateDTO;
import com.cheer.service.OaNoticeService;
import com.cheer.vo.OaNoticeInfoVO;
import com.cheer.vo.OaNoticeListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/notice")
public class OaNoticeController {

    @Autowired
    private OaNoticeService oaNoticeService;

    @ApiOperation("发布通知")
    @PostMapping("/add")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result add(@RequestBody @Valid OaNoticeAddDTO noticeAddDTO) {
        oaNoticeService.add(noticeAddDTO);
        return Result.success();
    }

    @ApiOperation("查询通知列表")
    @GetMapping("/list")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<Page<OaNoticeListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(oaNoticeService.list(pageNum, pageSize));
    }

    @ApiOperation("删除通知")
    @GetMapping("/delete")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delete(@RequestParam Integer id) {
        oaNoticeService.removeById(id);
        return Result.success();
    }

    @ApiOperation("查询通知详情")
    @GetMapping("/info")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<OaNoticeInfoVO> info(@RequestParam Integer id) {
        return Result.success(oaNoticeService.info(id));
    }

    @ApiOperation("修改通知")
    @PostMapping("/update")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result update(@RequestBody @Valid OaNoticeUpdateDTO noticeUpdateDTO) {
        oaNoticeService.update(noticeUpdateDTO);
        return Result.success();
    }
}
